package planit.view;

import java.util.Observable;
import java.util.Observer;

import javax.swing.SwingUtilities;

import planit.model.Model;
import planit.model.Schedule;

public class View implements Observer {
	private MainWindow mainWindow;
	private Model model;
	
	 private static View instance = null;
	 
	   protected View() {
	      // Exists to defeat instantiation
	   }
	
	public static View getInstance() {
	      if(instance == null) {
	         instance = new View();
	      }
	      return instance;
	   }
	
	public void setModel(Model model){
	
		this.model = model;
	}
	
	public void run() {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				mainWindow = new MainWindow(model);
				mainWindow.setLocationRelativeTo(null);
				mainWindow.setVisible(true);
				mainWindow.updateGroupList();
				mainWindow.updatePersonList();
				mainWindow.setTitle("PlanIT Prototype");			
			}
		});
	} 

	public void update(Observable o, Object arg) {
		Schedule.ChangeType type = (Schedule.ChangeType)arg;
		
		if (type == Schedule.ChangeType.EVENT) {
			mainWindow.updateSchedulePanes();
		} else if (type == Schedule.ChangeType.PERSON) {
			mainWindow.updatePersonList();
		} else if (type == Schedule.ChangeType.GROUP) {
			mainWindow.updateGroupList();
		} else if (type == Schedule.ChangeType.CHECKOUT) {
			mainWindow.updateSchedulePanes();
		}
	}
}
